﻿/* ************************************************************
 * JUST TEST CODE -- NOT READY FOR PRODUCTION
 * Author:  Roy Penrod
 * 
 * Controls the player's ship.
 * 
 * Implements the JumpFleetGameLib.xCamera.IMoveableObject 
 * interface.
 * 
 * ************************************************************ */

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace PlayerShip
{
    class PlayerShip : JumpFleetGameLib.xCamera.IMoveableObject
    {
        #region MEMBERS        

        // holds the static ship sprite
        private JumpFleetGameLib.xSprite.StaticSprite _sprite;

        // ship's position
        private Vector2 _currentPosition;

        // flag for whether the ship is visible
        private bool _visible;

        // ship's rotation angle
        private float _rotationAngle;

        // ship's flight speed
        // in pixels per second
        private float _shipFlightSpeed;

        // Xbox 360 thumbstick origin
        private Vector2 _thumbstickOrigin;          
    
        // holds the camera object
        private JumpFleetGameLib.xCamera.Camera2D _camera;

        #endregion


        #region PROPERTIES
                
        public Vector2 CurrentPosition
        {
            get { return _currentPosition; }

            set
            {                
                _currentPosition = value;
                // sets the sprite's DrawPosition to the current ship position
                _sprite.DrawPosition = value;                
            }
        }

        public bool Visible
        {
            get { return _visible; }

            set
            {
                // sets the sprite's Visible property to the same as the ship's visible flag
                _visible = value;
                _sprite.Visible = value;
            }
        }

        public float RotationAngle
        {
            get { return _rotationAngle; }

            set
            {
                // sets the sprite's RotationAmount property to the ship's rotation angle
                _rotationAngle = value;
                _sprite.RotationAmount = value;
            }
        }

        public float FlightSpeed
        {
            get { return _shipFlightSpeed; }

            set { _shipFlightSpeed = value; }
        }

        // implemented from xCamera.IMoveableObject interface
        public Vector2 CameraAnchorPosition
        {
            get { return _currentPosition; }
        }

        // implemented from xCamera.IMoveableObject interface
        public Rectangle BoundingBox
        {
            get { return new Rectangle((int)_sprite.DrawPosition.X, (int)_sprite.DrawPosition.Y, _sprite.Width, _sprite.Height); }

        }

        public JumpFleetGameLib.xCamera.Camera2D Camera
        {
            get { return _camera; }

            set { _camera = value; }
        }

        #endregion


        #region CONSTRUCTORS

        // templateName is the static sprite template name
        // Visible defaults to false
        public PlayerShip(JumpFleetGameLib.xSprite.StaticSprite sprite, float shipFlightSpeed)
        {
            _sprite = sprite;            
            Visible = false;
            _shipFlightSpeed = shipFlightSpeed;
            _thumbstickOrigin = new Vector2(0, 0);            
        }

        #endregion


        #region METHODS

        // controls the ship's movement
        public void Update(GameTime gameTime, GamePadState gamePadState)
        {
            // gets the thumbstick position out of the gamepad state
            float thumbstickX = gamePadState.ThumbSticks.Left.X;
            float thumbstickY = gamePadState.ThumbSticks.Left.Y;
            Vector2 movementDistance = new Vector2(0, 0);

            // checks to see if the thumbstick is in the neutral position
            // if it is, ignore it
            if ((thumbstickX != 0) || (thumbstickY != 0))
            {
                // computes the rotation angle of the thumbstick
                float rotationAngle = (float)Math.Atan2(thumbstickX, thumbstickY);

                // sets the ship's rotation
                RotationAngle = rotationAngle;

                // figures out the ship's movement 
               // Vector2 movementDistance = new Vector2(0, 0);
                movementDistance.X = (float)(thumbstickX * _shipFlightSpeed * gameTime.ElapsedGameTime.TotalSeconds);
                movementDistance.Y = (float)-(thumbstickY * _shipFlightSpeed * gameTime.ElapsedGameTime.TotalSeconds);

                System.Diagnostics.Debug.WriteLine(movementDistance.ToString());

                // sends the movement distance to the camera
                _camera.MoveObject(movementDistance, true);             
            }

            _camera.MoveObject(movementDistance, true);             

        }

        // move the player ship by the movement distance
        // implements the xCamera.IMoveableObject interface
        public void Move(Vector2 movementDistance)
        {
            CurrentPosition = new Vector2(CurrentPosition.X + movementDistance.X, CurrentPosition.Y + movementDistance.Y);
        }

        #endregion

    } // <-- end class
} // <-- end namespace
